package com.copa.l215;

import java.util.PriorityQueue;

/**
 * @author copa
 * @createDate 2022-05-29 18:30
 * @function 堆排序法（最小堆）
 */
public class Test2 {

    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int num : nums) {
            pq.add(num);
            if (pq.size() > k) {
                pq.poll();
            }
        }
        return pq.peek();
    }

    public static void main(String[] args) {
        int[] nums = {3,2,1,5,6,4};
        System.out.println(new Test2().findKthLargest(nums, 2));
    }
}
